// Actually Nino should look here: http://www.xml.com/pub/rg/XML_Parsers (!)    -- xPablo
// Actually nino can write his own ;)                                           -- Nino_a
// Actually there is NO usable xml parser...                                    -- xPablo
// despite i write my own one :P                                                -- Nino_a

#include "xml.h"

int xml_last_id( char *xml, char *last_id ) {
	int s_len = strlen( xml );
	int i, j, k; // counters
	
	for( i = s_len; i > 0; i-- ) {
		if( xml[ i ] == ' ' && xml[ i+1] == 'i' && xml[ i+2] == 'd' && xml[ i+3 ] == '=' ) {
			i += 5; // i now is the starting point of the id
			break;
		}
	}
	
	for( j=1; xml[ i+j ] != '"'; j++ );
	
	for( k=0; k < j; k++ )
		last_id[ k ] = xml[ i+k ];
		
	return 0;
}

int increment_single( char *c ) {

/* If 0 is returned, everything went fine
 * if 1 is returned, c is set to 'a' and the next char has to be increased by one
 * if -1 is returned, it failed ;) */
 
 if( *c >= 'a' && *c < 'z' ) {
		*c += 1;
		return 0;
	}
	else if( *c == 'z' ) {
		*c= 'A';
		return 0;
	}
	else if( *c >= 'A' && *c < 'Z' ) {
		*c += 1;
		return 0;
	}
	else if( *c == 'Z' ) {
		*c = '0';
		return 0;
	}
	else if( *c >= '0' && *c < '9' ) {
		*c += 1;
		return 0;
	}
	else if( *c == '9' ) {
		*c = 'a';
		return 1;
	}
	else {
		return -1;
	}
}

int xml_increment_id( char *id ) {
	if( !id ) return -1;
	int len, i;
	len = strlen( id );
	i = len -1;
	if( len < 1 ) return -1;
	
	while( (increment_single( &id[ i]) ) == 1 ) {
		if( i > 0 )
			i-=1;
		else {
			id[  len  ] =  'a'; // error: if length is increased, first letter is 'b', but should be 'a'
			id[ len+1 ] = '\0';
		}
	}
	
	return 0;
}

int xml_get_url( char *xml, char *id, char *addr ) { 
	if( !id || !xml || !addr ) return -1;
	int xmllen = strlen( xml );
	int idlen = strlen( id );
	int i, j;
	
	for( i = xmllen; i > 0; i-- ) {
		if( ( xml[i-5] == ' ' ) &&
			  ( xml[i-4] == 'i' ) &&
			  ( xml[i-3] == 'd' ) &&
			  ( xml[i-2] == '=' ) &&
			  ( xml[i-1] == '"' ) &&
			  ( xml[ i ] == id[0] ) ) {
			for( j=0; id[j] == xml[i+j]; j++ );
			if( j== idlen && xml[i+j] == '"' ) {
				j = i + idlen +2;
				i= 0;
				for( ;j < xmllen ; i++, j++ ) {
					if( (xml[ j ] == '<' ) &&
					    (xml[j+1] == '/' ) &&
					    (xml[j+2] == 'u' ) &&
					    (xml[j+3] == 'r' ) &&
					    (xml[j+4] == 'l' ) &&
					    (xml[j+5] == '>' ) &&
					    (xml[j+6] == '\n') )
						break;
					addr[i ] = xml[j ];
				}
				break;
				return 0;
			}
		}
	}
  return -1;
}

int xml_get_id( char *xml, char *addr, char *id ) {
	if( !xml ) return -1;
	if( !addr ) return -1;
	if( !id ) return -1;
	
	int i, j, idlen, addrlen, xmllen;
	
	addrlen = strlen( addr );
	xmllen = strlen( xml );
	
	for( i=xmllen; i > 0; i-- ) {
		if( ( xml[i-2]== '"' ) &&
		    ( xml[i-1]== '>' ) &&
		    ( xml[ i] == addr[0] ) )
		{
			if( ( xml[ i + addrlen ] == '<' ) &&
			    ( xml[ i + addrlen + 1 ] == '/' ) )
			{
				for( j=0; xml[ i + j ] == addr[ j ]; j++ );
				if( j == addrlen ) {
					i -= 2;
					for( idlen=0; xml[ i - idlen - 1] != '"'; idlen++ );
					i -= idlen;
					for( j=0; j< idlen; j++ )
						id[ j] = xml[ i+j ];
				}
			}
		}
	}
	if( idlen == 1 )
		id[ 1 ] = '\0';
  return 0;
}

